<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8"/>
    <title>填写卡密权益</title>
    <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no">
</head>
<style>
    .container {
        display: flex;
        flex-direction: column;
        padding: 0 10px;
    }

    .label {
        font-weight: 600;
        margin-top: 8px;
        font-size: 16px;
    }

    select, input {
        font-size: 14px;
        height: 40px;
        margin-top: 6px;
        padding: 0 10px;
        border: 1px solid #d9d9d9;
        border-radius: 4px
    }

    /*input {*/
    /*    font-size: 14px;*/
    /*    height: 40px;*/
    /*    margin-top: 6px;*/
    /*    padding: 0 10px;*/
    /*    border: 1px solid #d9d9d9;*/
    /*    border-radius: 4px*/
    /*}*/

    button {
        height: 44px;
        margin-top: 10px;
        background-color: #0E8CFE;
        color: white;
        border-radius: 5px;
    }

    span {
        color: red;
        font-size: 12px;
    }

    .hide {
        display: none;
    }

    select{
        background: transparent;
        color:rgb(118, 118, 118);
    }
</style>
<body>
<div class="container" id = "content" style="min-height:600px">

</div>
<script src="/url.js"></script>
<script src="/jquery-3.5.1.min.js"></script>


<script src="/app.js"></script>
<script src='/js/alert.js'></script>


<script type="text/javascript">
    let ids = new URLSearchParams(location.href.split('?')[1]).get("ids")

    let orderInfo

    getNumber()

    /**
     * 提交数据
     */
    function postData() {
        document.getElementById("postBtn").disabled = true;
        let paramInfo = {}
        if(orderInfo.goodsType == 1){
            let logisticsNumber = $("#logisticsNumber").val();
            if(logisticsNumber == ''){
                alert('快递单号不能为空')
                document.getElementById("postBtn").disabled = false;
                return
            }
            paramInfo.logisticsNumber = logisticsNumber
        }else{
            for (let i = 1; i <= orderInfo.number; i ++){
                let key_cardNum = "cardNum_"+i
                let key_cardPassword = "cardPassword_"+i
                let key_rightsUrl = "rightsUrl_"+i

                let cardNum = $("#"+key_cardNum).val();
                let cardPassword = $("#"+key_cardPassword).val();
                let rightsUrl = $("#"+key_rightsUrl).val();
                if(cardNum == '' || cardPassword == '' || rightsUrl == ''){
                    alert('卡密权益'+i+'不能为空')
                    document.getElementById("postBtn").disabled = false;
                    return
                }
                paramInfo[key_cardNum] = cardNum;
                paramInfo[key_cardPassword] = cardPassword;
                paramInfo[key_rightsUrl] = rightsUrl;
            }
        }
        paramInfo['orderId'] = ids

        let url = requestUrl + "/erupt-api/order/addLogistics";
        $.ajax({
            type: "POST",
            url: url,
            headers: {
                token: parent.getAppToken().token
            },
            data: {
                content: JSON.stringify(paramInfo)
            },
            datatype: "json",
            success: function (result) {
                console.log(result)
                if (result.code == 1) {
                    alert("发货成功")
                    parent.document.querySelector(".ant-modal-close-x").click()
                } else {
                    alert(result.msg)
                }
                document.getElementById("postBtn").disabled = false;
            }
        });

    }

    /**
     * 获取商品数量
     */
    function getNumber() {
        let url = requestUrl + "/erupt-api/order/getNumber";

        $.ajax({
            type: "POST",
            url: url,
            headers: {
                token: parent.getAppToken().token
            },
            data: {orderId: ids},
            datatype: "json",
            success: function (result) {
                console.log(result)

                if(result.code == 1){
                    orderInfo = result.data
                    //初始化页面
                    let html_str = ''
                    if(result.data.goodsType == 1){
                        html_str = html_str+'<div class="label">快递单号信息</div>\n' +
                            '    <input id="logisticsNumber" type="text" placeholder="快递单号(必填)"/>';
                    }else{
                        for (let i = 1; i <= result.data.number;i ++ ){
                            html_str=html_str+'<div class="label">卡密权益信息_'+i+'</div>\n' +
                                '    <input id="cardNum_'+i+'" type="text" autocomplete="off" placeholder="卡号(必填)"/>\n' +
                                '    <input id="cardPassword_'+i+'" type="text" autocomplete="off" placeholder="卡密(必填)"/>\n' +
                                '    <input id="rightsUrl_'+i+'" type="text" autocomplete="off" placeholder="权益链接(必填)"/>'
                        }
                    }
                    html_str = html_str + '<button id="postBtn" onclick="postData()">提交</button>';
                    document.getElementById('content').innerHTML = html_str
                }
            }
        });
    }

</script>
</body>
</html>
